/*
Date:20220621 13:59PM
key:注意GO的复杂类型传值都是引用，也就是想要new要通过make。
*/
type NumArray struct {
    arr *[]int
    sum *[]int
}

func Show(arr *[]int,sum *[]int){
    tmparr:=*arr
    tmpsum:=*sum
    fmt.Println("arr:")
    for _,val:=range tmparr{
        fmt.Println(val)
    }
    fmt.Println("sum:")
    for _,val:=range tmpsum{
        fmt.Println(val)
    }

}
func Constructor(nums []int) NumArray {
    var arr []int
    var sum []int
    arr=nums
    sum=make([]int,len(nums))
    for i:=0;i<len(nums);i++{
        if i==0{sum[i]=arr[i]
        continue}
        sum[i]=sum[i-1]+nums[i]
    }
    return NumArray{
        &arr,
        &sum,
    }

}


func (this *NumArray) Update(index int, val int)  {
    tmparr:=*this.arr
    tmpsum:=*this.sum
    d:=val-tmparr[index]
    tmparr[index]=val
    for i:=index;i<len(tmpsum);i++{
        tmpsum[i]+=d
    }
    this.arr=&tmparr
    this.sum=&tmpsum
}


func (this *NumArray) SumRange(left int, right int) int {
    tmpsum:=*this.sum
    if left-1<0{
        return tmpsum[right]
    }
    return tmpsum[right]-tmpsum[left-1]
}



/**
 * Your NumArray object will be instantiated and called as such:
 * obj := Constructor(nums);
 * obj.Update(index,val);
 * param_2 := obj.SumRange(left,right);
 */